package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishCategoryVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RequestMapping("/admin/dish")
@RestController
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品参数:{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        log.info("清空菜品的Redis数据");
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }


    /**
     * 分页查询菜品列表
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品列表")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品列表参数:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除选中菜品")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除菜品的ID:{}", ids);
        dishService.delete(ids);

        log.info("清空菜品的Redis数据");
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据菜品id查询菜品信息和口味信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取菜品信息")
    public Result<DishVO> getInfoByid(@PathVariable Long id) {
        log.info("查询菜品ID：{}", id);
        DishVO dishVO = dishService.getInfoByid(id);

        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息和对应的口味信息
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品信息和对应的口味信息")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品参数：{}", dishDTO);
        dishService.update(dishDTO);

        log.info("清空菜品的Redis数据");
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");

        return Result.success();
    }


    /**
     * 根据分类id查询菜品信息
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id获取菜品信息")
    public Result<List<DishCategoryVO>> getInfoByCategoryId(Long categoryId) {
        log.info("分类ID：{}", categoryId);

        List<DishCategoryVO> dishCategoryVOList = dishService.getInfoByCategoryId(categoryId);

        return Result.success(dishCategoryVOList);
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        dishService.startOrStop(status,id);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        log.info("清空菜品的Redis数据");
        cleanCache("dish_*");

        return Result.success();
    }
}

